package com.ht.module.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ht.abnormal.HtException;
import com.ht.constant.DicConstant;
import com.ht.constant.ErrorConstant;
import com.ht.module.bus.dto.RoomRuleChangeDto;
import com.ht.module.bus.dto.ScorePointsDto;
import com.ht.module.bus.entity.BusRoom;
import com.ht.module.bus.entity.BusRoomRecord;
import com.ht.module.bus.entity.BusRoomUser;
import com.ht.module.bus.mapper.BusRoomMapper;
import com.ht.module.bus.mapper.BusRoomUserMapper;
import com.ht.module.bus.service.BusRoomRecordService;
import com.ht.module.bus.service.BusRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.bus.service.BusRoomUserService;
import com.ht.module.bus.vo.ScoreChangeVo;
import com.ht.module.sys.entity.SysUser;
import com.ht.module.sys.mapper.SysUserMapper;
import com.ht.module.bus.vo.ShowUserVo;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import com.ht.util.UserUtil;
import com.ht.webSocket.MessageResult;
import com.ht.webSocket.SocketServer;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 房间表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-11-25
 */
@Service
public class BusRoomServiceImpl extends ServiceImpl<BusRoomMapper, BusRoom> implements BusRoomService {
    @Resource
    private BusRoomUserMapper busRoomUserMapper;
    @Resource
    private BusRoomUserService busRoomUserService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private BusRoomRecordService busRoomRecordService;

    //定义锁map
    private final ConcurrentHashMap<Integer, Object> roomLockMap = new ConcurrentHashMap<>();

    /**
     * 获取锁方法
     * @param roomId
     * @return
     */
    private Object getRoomLock(Integer roomId) {
        return roomLockMap.computeIfAbsent(roomId, k -> new Object());
    }


    @Override
    public void joinRoom(Integer roomId) {
        String userId = UserUtil.getUserId();
        if (StrUtil.isEmpty(userId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }

        //判断房间是否存在
        BusRoom busRoom = baseMapper.selectById(roomId);
        if (BeanUtil.isEmpty(busRoom)) {
            throw new HtException(ErrorConstant.ROOM_NOT_EXIST);
        }

        //判断房间已经开始
        if (busRoom.getIsStart() == 1) {
            throw new HtException(ErrorConstant.ROOM_START_ERROR);
        }

        //判断房间已经结束
        if (busRoom.getIsStart() == 2) {
            throw new HtException(ErrorConstant.ROOM_END_ERROR);
        }

        //判断自己是否已经存在房间
        Long i = busRoomUserMapper.selectCount(
                new QueryWrapper<BusRoomUser>()
                        .eq("user_id", userId)
                        .eq("room_id", roomId)
        );

        //不存在当前房间，插入一条
        if (i == 0) {
            //查询出当前房间
            BusRoomUser roomUser = new BusRoomUser()
                    .setRoomId(roomId)
                    .setUserId(userId)
                    .setIsHost(1)
                    .setCreateDate(LocalDateTime.now());
            busRoomUserMapper.insert(roomUser);
        }


        //发送消息，像所有连接了当前房间socket的用户发送消息
        SysUser user = UserUtil.getUser();
        ShowUserVo vo = new ShowUserVo().setUserId(user.getId())
                .setName(user.getName())
                .setAvatar(user.getAvatar())
                .setIsHost(1);

        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.joinRoom)
                .setData(vo)
                .setSendUserId(userId);
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + roomId);


    }

    @Override
    public void quitRoom(Integer roomId) {
        String userId = UserUtil.getUserId();
        //房客权限判断
        lodgerAuthJudgment(roomId);


        //移除
        int delete = busRoomUserMapper.delete(
                new QueryWrapper<BusRoomUser>()
                        .eq("room_id", roomId)
                        .eq("user_id", userId)
                        .eq("is_host", 1)
        );
        //退出成功才发消息
        if (delete == 1) {
            MessageResult messageResult = new MessageResult()
                    .setType(DicConstant.MessageType.quitRoom)
                    .setData(userId)
                    .setSendUserId(userId);
            //群发消息
            SocketServer.sendInfoType(messageResult, "" + roomId);
        }
    }

    @Override
    public BusRoom createRoom(BusRoom en) {
        String userId = UserUtil.getUserId();
        if (StrUtil.isEmpty(userId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }
        if (BeanUtil.isEmpty(en.getIsStart())) {
            //默认创建的房间未开始
            en.setIsStart(0);
        }
        if (BeanUtil.isEmpty(en.getMode())) {
            //设置房间计分模式
            en.setMode(0);
        }
        if (BeanUtil.isEmpty(en.getTaiwanFee())) {
            //设置台费规则-无台费
            en.setTaiwanFee(0);
        }
        if (BeanUtil.isEmpty(en.getFullVal())) {
            //设置抽分规则-满多少抽多少
            en.setFullVal(new BigDecimal(8));
        }
        if (BeanUtil.isEmpty(en.getDeductVal())) {
            //设置抽分规则-满多少抽多少
            en.setDeductVal(new BigDecimal(1));
        }
        //创建一个房间
        baseMapper.insert(en);
        //房主加入房间
        busRoomUserMapper.insert(
                new BusRoomUser().setRoomId(en.getRoomId())
                        .setUserId(userId)
                        .setIsHost(0)
                        .setCreateDate(LocalDateTime.now())
        );
        SysUser user = UserUtil.getUser();
        en.getRoomUsers().add(
                new ShowUserVo().setName(user.getName())
                        .setAvatar(user.getAvatar())
                        .setUserId(userId)
                        .setIsHost(0)
        );
        return en;
    }

    @Override
    public void dissolveRoom(Integer roomId) {
        //判断是否有房主权限
        BusRoom busRoom = hostAuthJudgment(roomId);
        //判断房间状态
        if (busRoom.getIsStart() != 0) {
            throw new HtException(ErrorConstant.ROOM_STATUS_ERROR);
        }

        //直接删除房间
        baseMapper.delete(
                new QueryWrapper<BusRoom>()
                        .eq("room_id", roomId)
        );
        //删除房间中的用户
        busRoomUserMapper.delete(
                new QueryWrapper<BusRoomUser>()
                        .eq("room_id", roomId)
        );


        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.dissolveRoom)
                .setSendUserId(UserUtil.getUserId());
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + roomId);
    }

    @Override
    public BusRoom findNoStartRoomByUser() {
        String userId = UserUtil.getUserId();
        if (StrUtil.isEmpty(userId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }
        List<BusRoom> busRooms = baseMapper.selectList(
                new QueryWrapper<BusRoom>()
                        .eq("is_start", 0)
                        .eq("create_user", userId)
                        .orderByDesc("create_date")
        );
        if (!busRooms.isEmpty()) {
            return busRooms.get(0);
        } else {
            return null;
        }
    }

    @Override
    public BusRoom findById(Integer id) {
        BusRoom busRoom = baseMapper.selectById(id);
        busRoom.setRoomUsers(busRoomUserService.findRoomUserByRoomId(id));
        return busRoom;
    }

    @Override
    public void startScoring(RoomRuleChangeDto en) {
        //房主权限判断
        BusRoom busRoom = hostAuthJudgment(en.getRoomId());
        //判断房间状态
        if (busRoom.getIsStart() != 0) {
            throw new HtException(ErrorConstant.ROOM_STATUS_ERROR);
        }
        //查询房间人数
        List<BusRoomUser> roomUsers = busRoomUserMapper.selectList(
                new QueryWrapper<BusRoomUser>()
                        .eq("room_id", en.getRoomId())
        );
        if (roomUsers.size() < 2) {
            throw new HtException(ErrorConstant.ROOM_UNDERNUMBER_ERROR);
        }
        //设置房间状态
        busRoom.setIsStart(1);
        CommMethod.beanUpdate(busRoom);
        //设置积分模式
        busRoom.setMode(ObjectUtil.isNotEmpty(en.getMode()) ? en.getMode() : busRoom.getMode());
        //设置台费规则
        //判断房间模式
        busRoom.setTaiwanFee(ObjectUtil.isNotEmpty(en.getTaiwanFee()) ? en.getTaiwanFee() : busRoom.getTaiwanFee());
        busRoom.setFullVal(ObjectUtil.isNotEmpty(en.getFullVal()) ? en.getFullVal() : busRoom.getFullVal());
        busRoom.setDeductVal(ObjectUtil.isNotEmpty(en.getDeductVal()) ? en.getDeductVal() : busRoom.getDeductVal());
        baseMapper.updateById(busRoom);
        //发送消息
        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.startRoom)
                .setSendUserId(UserUtil.getUserId());
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + en.getRoomId());
    }

    @Override
    public Pager<BusRoom> findStartRoomByTime(Pager<BusRoom> pager, BusRoom en) {
        String userId = UserUtil.getUserId();
        if (StrUtil.isEmpty(userId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }
        QueryWrapper<BusRoom> qw = getQW(en);
        qw.orderByDesc("create_date");
        baseMapper.findStartRoomByUser(pager, qw, userId);

        if (!pager.getRecords().isEmpty()) {
            //构建出当前房间的所有参与玩家
            List<BusRoomUser> roomUsers = busRoomUserMapper.selectList(
                    new QueryWrapper<BusRoomUser>()
                            .in("room_id", pager.getRecords().stream().map(BusRoom::getRoomId).collect(Collectors.toList()))
            );
            //查询出当前所有用户
            if (!roomUsers.isEmpty()) {
                List<SysUser> sysUsers = sysUserMapper.selectBatchIds(roomUsers.stream().map(BusRoomUser::getUserId).collect(Collectors.toList()));
                //构建用户
                for (BusRoom record : pager.getRecords()) {
                    //构建每一个房间的用户
                    List<BusRoomUser> tempRoomUsers = roomUsers.stream().filter(x -> ObjectUtil.equals(x.getRoomId(), record.getRoomId())).collect(Collectors.toList());
                    //查询出当前用户
                    List<ShowUserVo> list = new ArrayList<>();
                    for (BusRoomUser tempRoomUser : tempRoomUsers) {
                        ShowUserVo vo = new ShowUserVo()
                                .setIsHost(tempRoomUser.getIsHost())
                                .setUserId(tempRoomUser.getUserId());
                        //查询出当前用户
                        SysUser sysUser = sysUsers.stream().filter(y -> StrUtil.equals(tempRoomUser.getUserId(), y.getId())).findFirst().orElse(null);
                        if (ObjectUtil.isNotEmpty(sysUser)) {
                            vo.setAvatar(sysUser.getAvatar());
                            vo.setName(sysUser.getName());
                        } else {
                            vo.setName("用户已注销");
                        }
                        ShowUserVo apply = vo;
                        list.add(apply);
                    }
                    record.setRoomUsers(list);
                }
            }
        }
        return pager;
    }

    @Override
    public void kickOutRoomById(Integer roomId, String userId) {
        //房主权限判断
        hostAuthJudgment(roomId);
        //踢出的用户不能是自己
        if (userId == UserUtil.getUserId()) {
            throw new HtException("不能将自己提出房间！");
        }
        int delete = busRoomUserMapper.delete(
                new QueryWrapper<BusRoomUser>()
                        .eq("user_id", userId)
                        .eq("room_id", roomId)
        );

        //发送消息
        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.kickOutRoom)
                .setSendUserId(UserUtil.getUserId())
                .setData(userId);   //设置被踢出人的id
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + roomId);
    }

    @Override
    @Transactional
    public void givePoolScore(Integer roomId, BigDecimal score) {
        //房客权限判断
        BusRoom busRoom = lodgerAuthJudgment(roomId);
        //判断房间状态
        if (busRoom.getIsStart() != 1) {
            throw new HtException("房间不在进行状态中！");
        }
        synchronized (getRoomLock(roomId)){
            //修改积分池分数
            busRoom.setScorePool(busRoom.getScorePool().add(score));

            //修改积分池得分
            this.update(
                    new UpdateWrapper<BusRoom>()
                            .eq("room_id", busRoom.getRoomId())
                            .setSql("score_pool = score_pool + " + score)
            );

            //更新房客得分
            busRoomUserService.update(
                    new UpdateWrapper<BusRoomUser>()
                            .eq("room_id", roomId)
                            .eq("user_id", UserUtil.getUserId())
                            .setSql("score = score - " + score)
            );
            //创建流水记录
            busRoomRecordService.save(
                    new BusRoomRecord()
                            .setScore(score)
                            .setCreateDate(LocalDateTime.now())
                            .setRoomId(roomId)
                            .setActionType(DicConstant.ActionType.GIVE_POOL)
                            .setActionUser(UserUtil.getUserId())
            );
            //发送分数变化消息
            sendScoreChangeMessage(busRoom);
        }
    }

    @Override
    @Transactional
    public void takePoolScore(Integer roomId, BigDecimal score) {

        //房客权限判断
        BusRoom busRoom = lodgerAuthJudgment(roomId);

        //获取台费
        BigDecimal table = taiwanFeeCompute(busRoom, score);
        //设置台费，用于发送消息
        busRoom.setTaiwanFeeValue(busRoom.getTaiwanFeeValue().add(table));

        //判断房间状态
        if (busRoom.getIsStart() != 1) {
            throw new HtException("房间不在进行状态中！");
        }

        //判断积分池中的分数是否大于当前分数
        if (busRoom.getScorePool().compareTo(score) < 0) {
            throw new HtException(ErrorConstant.UNDERSCORE_ERROR);
        }

        synchronized (getRoomLock(roomId)){
            //修改积分池分数
            busRoom.setScorePool(busRoom.getScorePool().subtract(score));
            //修改积分池得分
            this.update(
                    new UpdateWrapper<BusRoom>()
                            .eq("room_id", busRoom.getRoomId())
                            .setSql("score_pool = score_pool - " + score)
            );
            //更新房客得分
            busRoomUserService.update(
                    new UpdateWrapper<BusRoomUser>()
                            .eq("room_id", roomId)
                            .eq("user_id", UserUtil.getUserId())
                            .setSql("score = score + " + (score.subtract(table)))
            );
            //创建流水记录
            busRoomRecordService.save(
                    new BusRoomRecord()
                            .setScore(score)
                            .setCreateDate(LocalDateTime.now())
                            .setRoomId(roomId)
                            .setActionType(DicConstant.ActionType.TAKE_POOL)
                            .setTaiwanFee(table)
                            .setActionUser(UserUtil.getUserId())
            );

            //发送消息变化分数消息
            sendScoreChangeMessage(busRoom);
        }
    }

    @Override
    public void endGame(Integer roomId) {
        //房主权限判断
        BusRoom busRoom = hostAuthJudgment(roomId);

        //判断房间状态
        if (busRoom.getIsStart() != 1) {
            throw new HtException(ErrorConstant.ROOM_STATUS_ERROR);
        }

        //修改房间状态-结束状态
        busRoom.setIsStart(2);
        baseMapper.updateById(busRoom);


        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.endGame)
                .setSendUserId(UserUtil.getUserId());
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + roomId);
    }

    @Override
    public void roomRuleChangeMessage(RoomRuleChangeDto en) {
        //修改房间信息
        baseMapper.updateById(
                new BusRoom().setRoomId(en.getRoomId())
                        .setMode(en.getMode())
                        .setTaiwanFee(en.getTaiwanFee())
                        .setFullVal(en.getFullVal())
                        .setDeductVal(en.getDeductVal())
                        .setTaiwanFeeMax(en.getTaiwanFeeMax())
                        .setPercentValue(en.getPercentValue())
        );
        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.roomRuleChange)
                .setData(en)
                .setSendUserId(UserUtil.getUserId());
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + en.getRoomId());

    }

    @Override
    @Transactional
    public synchronized void givePoints(ScorePointsDto en) {
        BusRoom busRoom = lodgerAuthJudgment(en.getRoomId());
        //获取台费
        BigDecimal table = taiwanFeeCompute(busRoom, en.getScore());
        //设置台费，用于发送消息
        busRoom.setTaiwanFeeValue(busRoom.getTaiwanFeeValue().add(table));
        synchronized (getRoomLock(en.getRoomId())){
            //接收人加分
            busRoomUserService.update(
                    new UpdateWrapper<BusRoomUser>()
                            .eq("room_id", en.getRoomId())
                            .eq("user_id", en.getReceiverId())
                            .setSql("score = score + " + (en.getScore().subtract(table)))
            );
            //判断房间模式
            if (ObjectUtil.equals(busRoom.getMode(), 1)) {
                //给分模式-需要扣除当前登录用户积分
                busRoomUserService.update(
                        new UpdateWrapper<BusRoomUser>()
                                .eq("room_id", en.getRoomId())
                                .eq("user_id", UserUtil.getUserId())
                                .setSql("score = score - " + en.getScore())
                );
            }

            //创建流水记录
            busRoomRecordService.save(
                    new BusRoomRecord()
                            .setScore(en.getScore())
                            .setCreateDate(LocalDateTime.now())
                            .setPassivityUser(en.getReceiverId())
                            .setRoomId(en.getRoomId())
                            .setActionType(DicConstant.ActionType.GIVE_POOL)
                            .setTaiwanFee(table)
                            .setActionUser(UserUtil.getUserId())
            );

            //积分变化消息
            sendScoreChangeMessage(busRoom);
        }

    }

    @Override
    public void deductScore(ScorePointsDto en) {
        BusRoom busRoom = hostAuthJudgment(en.getRoomId());
        synchronized (getRoomLock(en.getRoomId())){
            //接收人减分
            busRoomUserService.update(
                    new UpdateWrapper<BusRoomUser>()
                            .eq("room_id", en.getRoomId())
                            .eq("user_id", en.getReceiverId())
                            .setSql("score = score - " + en.getScore())
            );
            //创建流水记录
            busRoomRecordService.save(
                    new BusRoomRecord()
                            .setScore(en.getScore())
                            .setCreateDate(LocalDateTime.now())
                            .setPassivityUser(en.getReceiverId())
                            .setRoomId(en.getRoomId())
                            .setActionType(DicConstant.ActionType.DEDUCT_SCORE)
                            .setActionUser(UserUtil.getUserId())
            );

            //积分变化消息
            sendScoreChangeMessage(busRoom);
        }

    }


    /**
     * 台费规则计算
     *
     * @param busRoom:当前房间信息
     * @param score：得分
     * @return 返回抽取的台费
     */
    private BigDecimal taiwanFeeCompute(BusRoom busRoom, BigDecimal score) {
        //判断房间类型-是否需要台费-台费规则：0：无台费，1：满x抽y，2：百分比
        if (ObjectUtil.equal(busRoom.getTaiwanFee(), 0)) {
            //0无需台费直接返回
            return new BigDecimal(0);
        }
        //判断当前台费是否达到最大台费
        if (busRoom.getTaiwanFeeValue().compareTo(busRoom.getTaiwanFeeMax()) >= 0) {
            //当前台费大于等于最大台费，则瞒住
            return new BigDecimal(0);
        }
        //台费规则为1的时候，需要抽取台费，但是未满足抽取分值，则不需要抽取
        if (ObjectUtil.equal(busRoom.getTaiwanFee(), 1) && score.compareTo(busRoom.getFullVal()) < 0) {
            return new BigDecimal(0);
        }
        //不满住以上的还需要抽取台费

        //获取剩余台费
        BigDecimal surplus = busRoom.getTaiwanFeeMax().subtract(busRoom.getTaiwanFeeValue());

        //定义待抽取的台费
        BigDecimal stay = new BigDecimal(0);
        //获取需要抽取的台费
        if (ObjectUtil.equal(busRoom.getTaiwanFee(), 1)) {
            //满x抽y-因为上述判断已经满足了X，直接抽取y
            stay = score.divideToIntegralValue(busRoom.getFullVal()).multiply(busRoom.getDeductVal());
        } else if (ObjectUtil.equal(busRoom.getTaiwanFee(), 2)) {
            //百分比
            stay = score.divide(new BigDecimal(100)).multiply(new BigDecimal(busRoom.getPercentValue()));
        } else {
            throw new HtException(ErrorConstant.RULE_ERROR);
        }
        //判断待抽取的台费是否大于剩余台费
        if (stay.compareTo(surplus) > 0) {
            //大于则待抽取台费等于剩余台费
            stay = surplus;
        }
        //修改剩余台费
        this.update(
                new UpdateWrapper<BusRoom>()
                        .eq("room_id", busRoom.getRoomId())
                        .setSql("taiwan_fee_value = taiwan_fee_value + " + stay)
        );


        return stay;
    }


    /**
     * 分数变化消息发送
     *
     * @param busRoom
     */
    private void sendScoreChangeMessage(BusRoom busRoom) {
        //获取当前房间用户的分数
        List<BusRoomUser> roomUsers = busRoomUserMapper.selectList(
                new QueryWrapper<BusRoomUser>()
                        .eq("room_id", busRoom.getRoomId())
        );
        //发送消息
        MessageResult messageResult = new MessageResult()
                .setType(DicConstant.MessageType.poolScoreChange)
                .setSendUserId(UserUtil.getUserId())
                //当前积分池分数
                .setData(
                        new ScoreChangeVo()
                                .setBusRoom(busRoom)
                                .setRoomUsers(roomUsers)
                );
        //群发消息
        SocketServer.sendInfoType(messageResult, "" + busRoom.getRoomId());
    }


    /**
     * 房客权限判断
     *
     * @param roomId
     */
    private BusRoom lodgerAuthJudgment(Integer roomId) {
        String loginUserId = UserUtil.getUserId();
        if (StrUtil.isEmpty(loginUserId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }

        //判断房间是否存在
        BusRoom busRoom = baseMapper.selectById(roomId);
        if (BeanUtil.isEmpty(busRoom)) {
            throw new HtException(ErrorConstant.ROOM_NOT_EXIST);
        }
        //判断是否存在当前房间
        List<BusRoomUser> roomUsers = busRoomUserMapper.selectList(
                new QueryWrapper<BusRoomUser>()
                        .eq("user_id", loginUserId)
                        .eq("room_id", roomId)
        );
        if (roomUsers.isEmpty()) {
            throw new HtException(ErrorConstant.NOT_ROOM_AUTHORITY);
        }
        return busRoom;
    }


    /**
     * 房主权限判断
     *
     * @param roomId
     */
    private BusRoom hostAuthJudgment(Integer roomId) {
        String loginUserId = UserUtil.getUserId();
        if (StrUtil.isEmpty(loginUserId)) {
            throw new HtException(ErrorConstant.PLEASE_LOGIN);
        }

        //判断房间是否存在
        BusRoom busRoom = baseMapper.selectById(roomId);
        if (BeanUtil.isEmpty(busRoom)) {
            throw new HtException(ErrorConstant.ROOM_NOT_EXIST);
        }

        //判断房间创建者是否当前用户
        if (!StrUtil.equals(loginUserId, busRoom.getCreateUser())) {
            throw new HtException(ErrorConstant.NOT_ROOM_AUTHORITY);
        }
        return busRoom;
    }


    /**
     * 构建基础查询条件
     *
     * @param en
     * @return
     */
    private QueryWrapper<BusRoom> getQW(BusRoom en) {
        QueryWrapper<BusRoom> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(en.getIsStart())) {
            qw.eq("tab.is_start", en.getIsStart());
        }
        if (ObjectUtil.isNotEmpty(en.getMode())) {
            qw.eq("tab.mode", en.getMode());
        }
        if (ObjectUtil.isNotEmpty(en.getTaiwanFee())) {
            qw.eq("tab.taiwan_fee", en.getTaiwanFee());
        }
        return qw;
    }

}
